import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Manager',
      component: () => import('@/views/Manager.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home', name: 'Home', component: () => import('@/views/manager/Home.vue'),
          beforeEnter: (to, from, next) => {
            debugger
            // 路由进入前的操作
            next()
          }
        },
        { path: 'user', name: 'User', component: () => import('@/views/manager/User.vue')},
        { path: 'location', name: 'Location', component: () => import('@/views/manager/Location.vue')},
        { path: 'byq', name: 'Byq', component: () => import('@/views/manager/Byq.vue')},
        { path: 'ddm', name: 'Ddm', component: () => import('@/views/manager/Ddm.vue')},
        { path: 'thd', name: 'Thd', component: () => import('@/views/manager/Thd.vue')},
        { path: 'xk', name: 'Xk', component: () => import('@/views/manager/Xk.vue')},
        { path: 'category', name: 'category', component: () => import('@/views/manager/Category.vue')},
        { path: 'xxs', name: 'Xxs', component: () => import('@/views/manager/Xxs.vue')},
        { path: 'gl', name: 'Gl', component: () => import('@/views/manager/Gl.vue')},
        // { path: 'fk', name: 'Fk', component: () => import('@/views/manager/Fk.vue')},
      ]
    },
    { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'),}
  ]
})


router.beforeEach((to, from, next) => {
  // 在这里进行权限验证、登录判断等操作
  /**
   * next()：允许路由跳转。
   * next(false)：取消路由导航。
   * next('/')：重定向到指定路径。
   * next({...})：重定向到指定路由对象
   */
  if(to.path.indexOf('/login')!=-1){
    next()
  }else{
    if (localStorage.getItem("token")==null || localStorage.getItem("token")=='') {
      next('/login') // 重定向到登录页面
    } else {
      next() // 允许路由跳转
    }
  }
})

router.afterEach(() => {
  // 跳转后的操作
})

router.beforeResolve((to, from, next) => {
  // 在导航被确认之前的操作
  next()
})

export default router
